const { mongoose } = require("../util/mongoose");

let Schema = mongoose.Schema({ //定义模型:作用就是规范字段名称 规范传递字段的数量
    username: { type: String, required: true },//必传
    lastLogin: { type: Object, require: true },//必传
    nowLogin: { type: Object, required: true }//必传
})

//创建集合 
let Collection = mongoose.model("logs", Schema);//创建集合 最好自己在集合名后加s
//增加内容到数据库
let addLog = (params) => {
    // 实例化集合
    let coll = new Collection(params)
    return coll.save()
        .then(res => res)
        .catch(err => {
            console.log(err)
            return false
        })
}

//查看登录日志   分页
let findLog = (query = {}, counts = { skip: 0, limit: 10 }) => {
    return Collection.find(query).sort({ _id: -1 }).skip(counts.skip).limit(counts.limit)
        .then(res => res)
        .catch(err => { console.log(err); return false })
}

//查看全部登录日志，可以查看单个人的所有登录日志
let findAllLog = (query = {}) => {
    return Collection.find(query).sort({ _id: -1 })
        .then(res => res)
        .catch(err => { console.log(err); return false })
}
module.exports = {
    addLog,
    findLog,
    findAllLog
}